package com.sgb.contract.view.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.BindingMethod;
import androidx.databinding.BindingMethods;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.sgb.contract.utils.ScreenUtil;
import com.swgk.core.util.MLog;

import org.jetbrains.annotations.NotNull;

import java.math.BigDecimal;

/**
 * 统一的自定义Glide功能的ImageView
 * @author cyj
 */
@BindingMethods({@BindingMethod(type = GlideImageView.class, attribute = "loadImageForUrl", method = "loadImageForUrl")})
public class GlideImageView extends androidx.appcompat.widget.AppCompatImageView {

    /** 原图宽高 */
    private int bitmapWidth, bitmapHeight;
    /** 实际图片显示的宽高 */
    private int intrinsicWidth, intrinsicHeight;

    public GlideImageView(Context context) {
        super(context);
    }

    public GlideImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public GlideImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public int getIntrinsicWidth() {
        return intrinsicWidth;
    }

    public int getIntrinsicHeight() {
        return intrinsicHeight;
    }

    /**
     * 根据url加载UI
     * @param url    url
     */
    public void loadImageForUrl(String url) {
        MLog.d(GlideImageView.class.getName(), "loadImageForUrl:" + url);
        if(null != url && !"".equals(url)) {
            try {
                Glide.with(getContext()).load(url)
                        .skipMemoryCache(true)
                        .diskCacheStrategy(DiskCacheStrategy.NONE)
                        .into(new CustomTarget<Drawable>() {
                            @Override
                            public void onResourceReady(@NonNull @NotNull Drawable resource, @Nullable @org.jetbrains.annotations.Nullable Transition<? super Drawable> transition) {
                                bitmapWidth = resource.getIntrinsicWidth();
                                bitmapHeight = resource.getIntrinsicHeight();
                                MLog.d("loadImageForUrl:原图宽高:", ""+bitmapWidth+" | "+bitmapHeight);
                                BigDecimal cale = BigDecimal.valueOf(bitmapWidth).divide(BigDecimal.valueOf(ScreenUtil.getScreenWidth(getContext())), 5, BigDecimal.ROUND_UP);
                                BigDecimal height = BigDecimal.valueOf(bitmapHeight).divide(cale,0, BigDecimal.ROUND_UP);
//                                BigDecimal height = BigDecimal.valueOf(ScreenUtil.getScreenWidth(getContext())).divide(new BigDecimal("0.70665"),0, BigDecimal.ROUND_UP);
                                MLog.d("loadImageForUrl:原图宽高11111:", ""+ScreenUtil.getScreenWidth(getContext())+" | "+height);
                                FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(ScreenUtil.getScreenWidth(getContext()), height.intValue());
                                setLayoutParams(lp);
                                setBackground(resource);
                                postInvalidate();
                            }

                            @Override
                            public void onLoadCleared(@Nullable @org.jetbrains.annotations.Nullable Drawable placeholder) {

                            }
                        });
            } catch (Throwable e) {
                MLog.e("loadImageForUrl:", "加载网络图片失败");
            }
        }
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        intrinsicWidth = getMeasuredWidth();
        intrinsicHeight = getMeasuredHeight();
        MLog.d("loadImageForUrl:实际图片宽高:", ""+intrinsicWidth+" | "+intrinsicHeight);
    }
}
